/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.framework.mvp

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast


/**
 * Author: czmc  Email: zxc6355533@163.com
 * Version:	1.0  Date:2018/3/1
 */

abstract class MvpFragment<P : MvpContract.Presenter> :Fragment(), MvpView {
    var rootView: View? = null
    var mPrensenter: P? = null
    abstract fun getLayoutId():Int

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView = inflater!!.inflate(getLayoutId(),null,false)
        return rootView
    }
    val loadding:LoadingDialog by lazy {
        LoadingDialog(context!!)
    }
    override fun context(): Context {
        return context!!
    }

    override fun showLoadding() {
        loadding.show()
    }

    override fun hideLodding() {
        if(loadding!=null){
            loadding.hide()
        }
    }

    override fun showToast(str: String) {
        Toast.makeText(context,str,Toast.LENGTH_SHORT).show()
    }
    override fun showEmpty(){

    }

    override fun showContent(){

    }

    override fun showError() {
    }
    private var mPresenter: P? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mPrensenter = getPresenter()
    }

    override fun onDestroy() {
        super.onDestroy()
        if(mPrensenter!=null){
            mPrensenter!!.onViewDestory()
        }
    }

    override fun showViewLoadding() {
    }
    protected abstract fun getPresenter(): P
}